﻿/*
	Copyright (C) 2019 Zongming Liu <1849059316@qq.com>

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License along
	with this program; if not, write to the Free Software Foundation, Inc.,
	51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#ifndef SpriteFram_h__
#define SpriteFram_h__

#include "../utility/MacroTool.h"
#include "../base/Rect.h"
#include <string>
#include <memory>

namespace SkyEngine2d
{
	
	class Texture2D;
	
	/*!
	 \brief 精灵帧
	 \details
	*/
	class SGE_API SpriteFrame
	{
		FRIEND_SMART_PTR(SpriteFrame)
	public:
	
		//! 析构
		/*!
		\return   
		*/~SpriteFrame();
		
		//! 从一张图片创建一个精灵帧     
		/*!
		\param const std::wstring & file_name  图片的名字  
		\return std::shared_ptr<SpriteFrame>   
		当图片被程序加载过一次，则会被缓存起来。多次调用别不会多次从磁盘加载图片。
		*/
		static std::shared_ptr<SpriteFrame> createFromPicture(const std::wstring &file_name);
		
		//! 从一张图片创建一个精灵帧 
		/*!
		\param const std::wstring & fileName  图片的名字
		\param const Rect & des_rect_in_tex  精灵在图片中的矩形区域 
		\return std::shared_ptr<SpriteFrame> 
		当图片被程序加载过一次，则会被缓存起来。多次调用别不会多次从磁盘加载图片。
		通过指定精灵帧在图片中的区域，方便将多个精灵打包到一个图片 。
		*/
		static std::shared_ptr<SpriteFrame> createFromPicture(const std::wstring & fileName, const Rect & des_rect_in_tex);
		
		//! 从纹理创建一个精灵帧
		/*!
		\param const std::shared_ptr<Texture2D> & testure 纹理
		\param const Rect & des_rect_in_tex 精灵帧在纹理中的矩形区域
		\return std::shared_ptr<SpriteFrame> 
		*/
		static std::shared_ptr<SpriteFrame> createFromTexture(const std::shared_ptr<Texture2D>& testure, const Rect & des_rect_in_tex);
		
		//! 获取精灵帧在纹理中的矩形位置
		/*!
		\return const Rect & 
		\sa void setRect(const Rect & rect)
		*/
		const Rect & getRect()const;
		 
		//! 设置精灵帧在纹理中的矩形区域
		/*!
		\param const Rect & rect 
		\return void 
		\sa const Rect & getRect()const
		*/
		void setRect(const Rect & rect);
	
		//! 获取精灵帧的纹理
		/*!
		\return std::weak_ptr<Texture2D> 
		*/
		std::weak_ptr<Texture2D> getTexture();
	
		//! 获取精灵帧的 尺寸 
		/*!
		\return const Size&
		*/
		const Size& getContentSize()const;

	protected:
	
		SpriteFrame();
	
		bool initWithTextureFileName(const std::wstring & file_name);
		bool initWithTextureFilename(const std::wstring & fileName, const Rect & rect);
		bool initWithTextureFilename(const std::wstring & fileName, const Rect & rect, bool rotated, const Vector2 offset, const Size &originalSize);
		bool initWithTexture(const std::shared_ptr<Texture2D> & texture, const Rect& rect, bool rotated, const Vector2 &offset, const Size & originalSize);
	
	private:
		
		Rect						m_rect;		  //frame 在纹理图片中的矩形位置 
		std::shared_ptr<Texture2D>	m_texture;	
		std::wstring				m_textureFileName;  
	};
}

#endif // SpriteFram_h__
